import { Component } from "@angular/core";
import {
  NavController,
  LoadingController,
  AlertController
} from "ionic-angular";
import { Storage } from "@ionic/storage";
import { HttpService } from "../../providers/http.service";
import { appConfig } from "../../providers/config";
import { CommonFunc } from "../../providers/commonFunc";
import { LoginPage } from "../login/login";

@Component({
  templateUrl: "set.html",
  providers: [HttpService, CommonFunc]
})
export class SetPage {
  private ip = "";
  private port = "";
  constructor(
    public navCtrl: NavController,
    private storage: Storage,
    private httpService: HttpService,
    private alert: AlertController,
    private loading: LoadingController
  ) {
    this.storage.get("ip").then(re => {
      if (re) {
        this.ip = re;
        this.storage.get("port").then(pd => {
          this.port = pd;
        });
      }
    });
  }

  doSubmit() {
    if (!this.port) {
      this.alert
        .create({
          title: "错误",
          message: "请输入域名",
          buttons: ["确认"],
          enableBackdropDismiss: true
        })
        .present();
      return;
    }
    if (!this.port) {
      this.alert
        .create({
          title: "错误",
          message: "请输入端口",
          buttons: ["确认"],
          enableBackdropDismiss: true
        })
        .present();
      return;
    }

    this.storage.set("ip", this.ip);
    this.storage.set("port", this.port);
    // 用于测试，不提交
    appConfig.IP_URL = "http://" + this.ip + ":" + this.port + "/";
    /*appConfig.IP_URL = "https://" + this.ip + ":" + this.port + "/";*/
    appConfig.PREVIEW_URL = "http://" + this.ip + ":8080/upload/";

    this.navCtrl.push(LoginPage).then(() => {
      this.navCtrl
        .getActive()
        .getNav()
        .remove(0);
    });
  }

  ionViewWillEnter() {}

  cancel() {
    this.navCtrl.pop();
  }
}
